package a66weding.com.jiehuntong.activity.home.hotel;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.adapter.MyBaseAdapter;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CancelCollectBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HomeHeaderLooperBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeShoppingCartDeleteBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.CircularImage;
import a66weding.com.jiehuntong.widgets.dialog.MyRedheartDialog;

/**
 * Created by admin on 2016/9/11.
 * 套餐的适配器
 */
public class Hoteladapter1 extends MyBaseAdapter implements OnNetResultListener {
    private HomeTaocanFragment1 mContext;
    private final int DIALOG = 1;
    private MyRedheartDialog myDialog;
    public static final int REQUSET_LOGIN_CODE = 100;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DIALOG:
                    if (myDialog != null) {
                        myDialog.dismiss();
                    }
                    break;

            }
        }
    };
    public int mCurrentBussid;
    public int mCurrentI_id;
    public boolean mCurrentCollect;
    public int mPosition;

    public Hoteladapter1(HomeTaocanFragment1 context) {
        super(context.getActivity(), null);
        this.mContext = context;
    }

    public void doCollect() {
        if (mCurrentCollect) {
            //取消收藏;
            String oldUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCANCEL);
            String url = oldUrl + "&productID=" + mCurrentI_id;
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    Hoteladapter1.this, CancelCollectBean.class, RequestCode.POSTCOLLECTIONCANCEL);
            HttpHandleUtil.executeCommonPost(context, httpRequestBean);
            ((HomeBotomListBean.DataBean) data.get(mPosition)).setIsCollection(false);
        } else {
            //添加收藏;
            String oldUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCREATE);
            String url = oldUrl + "&productID=" + mCurrentI_id + "&businessID=" + mCurrentBussid;
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    Hoteladapter1.this, MeShoppingCartDeleteBean.class, RequestCode.POSTCOLLECTIONCREATE);
            HttpHandleUtil.executeCommonPost(context, httpRequestBean);
            ((HomeBotomListBean.DataBean) data.get(mPosition)).setIsCollection(true);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_package, null);
            viewHolder.iv_homebusitem_business = (ImageView) convertView.findViewById(R.id.iv_homebusitem_business);
            viewHolder.item_pack_title = (TextView) convertView.findViewById(R.id.item_pack_title);
            viewHolder.tv_homelist_itemprice = (TextView) convertView.findViewById(R.id.tv_homelist_itemprice);
            viewHolder.tv_homelist_itemmarketprice = (TextView) convertView.findViewById(R.id.tv_homelist_itemmarketprice);
            viewHolder.ic_launcher = (CircularImage) convertView.findViewById(R.id.package_icon_item);
            viewHolder.pack_shoucang = (RelativeLayout) convertView.findViewById(R.id.pack_shoucang);
            viewHolder.iv_home_collect = (ImageView) convertView.findViewById(R.id.iv_home_collect);
            viewHolder.iv_homelist_toshoping = (LinearLayout) convertView.findViewById(R.id.iv_homelist_toshoping);
            viewHolder.pack_tv_hotelname = (TextView) convertView.findViewById(R.id.pack_tv_hotelname);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //添加数据
        final HomeBotomListBean.DataBean dataBean = (HomeBotomListBean.DataBean) data.get(position);

        //店铺图片
        x.image().bind(viewHolder.iv_homebusitem_business, dataBean.getNvc_PicMain(), ImageOptionsUtil.getOptions316_720());
        //店铺头像
        x.image().bind(viewHolder.ic_launcher, dataBean.getBusiness().getNvc_Logo(), ImageOptionsUtil.getcircle_fixy());
        //店铺名称
        viewHolder.pack_tv_hotelname.setText(dataBean.getBusiness().getNvc_StoreName());
        //详情说明
        viewHolder.item_pack_title.setText(dataBean.getNvc_Name());
        //售价
        viewHolder.tv_homelist_itemprice.setText(String.valueOf(dataBean.getDm_ShopPrice()));

        //原价
        viewHolder.tv_homelist_itemmarketprice.setText("" + dataBean.getDm_Price());
        //设置中划线 并高清;
        viewHolder.tv_homelist_itemmarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        //判断是否收藏了;
        final boolean isCollection = dataBean.isIsCollection();
        if (isCollection) {
            viewHolder.iv_home_collect.setImageResource(R.mipmap.storyshoucang_after);
        } else {
            viewHolder.iv_home_collect.setImageResource(R.mipmap.storyshoucang);
        }

        viewHolder.iv_homelist_toshoping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_id = dataBean.getI_Id();
                Intent intent = new Intent(context, HomeProductdetailActivity.class);
                intent.putExtra("productId", i_id);
                context.startActivity(intent);
            }
        });
        viewHolder.pack_shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isLogin = ((GlobalApplication) context.getApplicationContext()).isLogin();
                mCurrentBussid = dataBean.getI_Id();
                mCurrentI_id = dataBean.getI_Id();
                mCurrentCollect = isCollection;
                mPosition = position;

                //如果是收藏;
                if (isLogin) {
                    if (isCollection) {
                        //取消收藏;
                        String oldUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCANCEL);
                        String url = oldUrl + "&productID=" + dataBean.getI_Id();
                        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                                Hoteladapter1.this, CancelCollectBean.class, RequestCode.POSTCOLLECTIONCANCEL);
                        HttpHandleUtil.executeCommonPost(context, httpRequestBean);
                        dataBean.setIsCollection(false);
                    } else {
                        //添加收藏;
                        String oldUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCREATE);
                        String url = oldUrl + "&productID=" + dataBean.getI_Id() + "&businessID=" + dataBean.getI_Business();
                        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                                Hoteladapter1.this, MeShoppingCartDeleteBean.class, RequestCode.POSTCOLLECTIONCREATE);
                        HttpHandleUtil.executeCommonPost(context, httpRequestBean);
                        dataBean.setIsCollection(true);
                    }
                    notifyDataSetChanged();
                } else {
                    showLoginDialog();
                }
            }
        });

        viewHolder.iv_homebusitem_business.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i_id = dataBean.getI_Id();
                Intent intent = new Intent(mContext.getActivity(), HomeProductdetailActivity.class);
                intent.putExtra("productId", i_id);
                mContext.startActivity(intent);

            }
        });


        return convertView;
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof MeShoppingCartDeleteBean) {      // 加入收藏成功后
            myDialog = new MyRedheartDialog(context, "收藏成功!");
            myDialog.show();
            mHandler.sendEmptyMessageDelayed(DIALOG, 800);
            List<HomeBotomListBean.DataBean> hotelListData = (List<HomeBotomListBean.DataBean>) data;
            if (hotelListData != null) {
                for (int i = 0; i < hotelListData.size(); i++) {
                    int i_businessId = hotelListData.get(i).getI_Id();
                    if (mCurrentBussid == i_businessId) {
                        hotelListData.get(i).setIsCollection(true);
                    }
                }
            }
        } else if (data instanceof CancelCollectBean) {      // 删除收藏成功后
            myDialog = new MyRedheartDialog(context, "取消收藏成功!");
            myDialog.show();
            mHandler.sendEmptyMessageDelayed(DIALOG, 800);
            List<HomeBotomListBean.DataBean> hotelListData = (List<HomeBotomListBean.DataBean>) data;
            if (hotelListData != null) {
                for (int i = 0; i < hotelListData.size(); i++) {
                    int i_businessId = hotelListData.get(i).getI_Id();
                    if (mCurrentBussid == i_businessId) {
                        hotelListData.get(i).setIsCollection(false);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    public class ViewHolder {
        private ImageView iv_homebusitem_business;
        private TextView item_pack_title;
        private TextView tv_homelist_itemprice;
        private TextView tv_homelist_itemmarketprice;
        private CircularImage ic_launcher;
        private RelativeLayout pack_shoucang;
        private ImageView iv_home_collect;
        private LinearLayout iv_homelist_toshoping;
        private TextView pack_tv_hotelname;
    }

    public void showLoginDialog() {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(mContext.getActivity(), "亲，需要登录哦~", "去登录", "取消", R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(mContext.getActivity(), LoginActivity.class);
                intent.putExtra("isCanBack", true);
                mContext.startActivityForResult(intent, REQUSET_LOGIN_CODE);
                confirmDialog.dismiss();
            }

            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }

}
